<?php

/**
 * uniBoard API User Group
 *
 * Light and fast forum solution for web 2.0-like communities.
 *
 * @package		uniBoard
 * @author		Rafał Pitoń
 * @license		http://www.gnu.org/licenses/gpl-3.0.html
 * @since		Version 0.1
 * 
 * ------------------------------------------------------------------------
 * 
 * $Date: 2010-09-08 03:38:35 +0000 (Wed, 08 Sep 2010) $
 * $Revision: 60 $
 * $Author: rafio.xudb@gmail.com $
 */

// ------------------------------------------------------------------------

class api_user_group extends class_api{
	
	/**
	 * Main class pointer
	 *
	 * @var object
	 */
	
	private $appCore				= NULL;
	
	/**
	 * Group ID
	 *
	 * @var integer
	 */
	
	public $group_id 				= 0;
	
	/**
	 * Group System
	 *
	 * @var bool
	 */
	
	public $group_system			= false;
	
	/**
	 * Group Name
	 *
	 * @var string
	 */
	
	public $group_name				= NULL;
	
	/**
	 * Group Name Prefix
	 *
	 * @var string
	 */
	
	public $group_prefix			= NULL;
	
	/**
	 * Group Name Suffix
	 *
	 * @var string
	 */
	
	public $group_suffix			= NULL;
	
	/**
	 * Group Rank
	 *
	 * @var integer
	 */
	
	public $group_rank				= 0;
	
	/**
	 * Group Legend Position
	 *
	 * @var integer
	 */
	
	public $group_pos				= 0;
	
	/**
	 * Group Legend Hidden
	 *
	 * @var bool
	 */
	
	public $group_hidden			= false;
	
	/**
	 * Group Masks
	 *
	 * @var array
	 */
	
	public $group_masks				= array();
				
	// ------------------------------------------------------------------------

	/**
	 * Initialise Api
	 *
	 * @param object $appCore
	 * @param integer $target_id
	 */
	
	function __construct( $appCore, $target_id = 0){
					
		// Store appCore pointer
		$this -> appCore = $appCore;
	
		// Store Group ID
		$this -> group_id	= (integer) $target_id;
		
		// Autoload If Group ID is bigger than zero
		if ( $target_id > 0)
		{
			// Make select
			$appCore -> db -> query( 'SELECT *
			FROM ' . DB_PREFIX . 'user_groups
			WHERE group_id = \'' . $this -> group_id . '\'
			LIMIT 1');
			
			// Mask found
			if ( $group_result = $appCore -> db -> fetch_assoc())
			{
				$this -> data_loaded		= true;
				
				$this -> group_system		= $group_result['group_system'];
				$this -> group_name			= $group_result['group_name'];
				$this -> group_prefix		= $group_result['group_prefix'];
				$this -> group_suffix		= $group_result['group_suffix'];
				$this -> group_rank			= $group_result['group_rank'];
				$this -> group_pos			= $group_result['group_pos'];
				$this -> group_hidden		= $group_result['group_hidden'];
				$this -> group_masks		= (array) explode( ',', $group_result['group_masks']);
			}
			else
			{
				//give action an error
				$this -> setError( $appCore -> lang['acp_group_not_found']);
			}
			
			// Free result
			$appCore -> db -> freeResult();
		}
		
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Save API data to DB
	 *
	 */
	
	function saveData(){
		
		// Do mysql query only when we have changes, and no errors
		if ( !empty( $this -> changes) && !$this -> error)
		{	
			// Insert?
			if ( $this -> group_id == 0)
			{
				// Insert new record
				$this -> appCore -> db -> insert( $this -> changes, 'user_groups');
			
				// Store new mask id
				$this -> group_id = $this -> appCore -> db -> insertId();
			}
			else
			{
				// Update record
				$this -> appCore -> db -> update( $this -> changes, 'user_groups', 'group_id = \'' . $this -> group_id . '\'');
			
				// Bots ACL clean
				$this -> appCore -> setConfig( 'crawlers_acl_cache', '');
				
				// Users ACL Clean
				$this -> appCore -> db -> update( array( 'user_acl_cache' => ''), 'users');
			}
		
			// Flush cache
			class_cache::flushCache( 'user_groups');
			class_cache::flushCache( 'groups_masks');
		}
		
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Delete API data to DB
	 *
	 */
	
	function deleteData(){
		
		// Only for non-system groups
		if ( !$this -> group_system)
		{
			// Delete data
			$this -> appCore -> db -> delete( 'user_groups', 'group_id = \'' . $this -> group_id . '\'');
			$this -> appCore -> db -> delete( 'user_groups_members', 'group_id = \'' . $this -> group_id . '\'');
			
			// Bots ACL clean
			$this -> appCore -> setConfig( 'crawlers_acl_cache', '');
			
			// Users ACL Clean
			$this -> appCore -> db -> update( array( 'user_acl_cache' => ''), 'users');
			
			// Flush cache
			class_cache::flushCache( 'user_groups');
			class_cache::flushCache( 'groups_masks');
		}
		
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Set group_system
	 *
	 * @param bool $new_value
	 */
	
	function setGroupSystem( $new_value){
		
		// Store in object
		$this -> group_system = (bool) $new_value;
		
		// Add to made changes
		$this -> changes['group_system'] = $this -> group_system;
		
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Set group_name
	 *
	 * @param string $new_value
	 */
	
	function setGroupName( $new_value){
		
		if ( isset( $new_value[0]))
		{
			// Store in object
			$this -> group_name = $new_value;
			
			// Check max lenght
			if ( mb_strlen( $new_value) > 255)
			{
				// Set error
				$this -> setError( $this -> appCore -> lang['acp_group_name_too_long']);
			}
			else
			{
				// Add to made changes
				$this -> changes['group_name'] = $this -> appCore -> db -> strEscape( $this -> group_name);
			}
		}
		else
		{
			// Set error
			$this -> setError( $this -> appCore -> lang['acp_group_name_empty']);
		}
		
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Set group_prefix
	 *
	 * @param string $new_value
	 */
	
	function setGroupPrefix( $new_value){
		
		// Store in object
		$this -> group_prefix = $new_value;
		
		// Check max lenght
		if ( mb_strlen( $new_value) > 255)
		{
			// Set error
			$this -> setError( $this -> appCore -> lang['acp_group_prefix_too_long']);
		}
		else
		{
			// Add to made changes
			$this -> changes['group_prefix'] = $this -> appCore -> db -> strEscape( $this -> group_prefix);
		}
		
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Set group_suffix
	 *
	 * @param string $new_value
	 */
	
	function setGroupSuffix( $new_value){
		
		// Store in object
		$this -> group_suffix = $new_value;
		
		// Check max lenght
		if ( mb_strlen( $new_value) > 255)
		{
			// Set error
			$this -> setError( $this -> appCore -> lang['acp_group_suffix_too_long']);
		}
		else
		{
			// Add to made changes
			$this -> changes['group_suffix'] = $this -> appCore -> db -> strEscape( $this -> group_suffix);
		}
		
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Set group_rank
	 *
	 * @param integer $new_value
	 */
	
	function setGroupRank( $new_value){
		
		// Store in object
		$this -> group_rank = (integer) $new_value;
		
		// ID is smaller than ZERO?
		if ( $this -> group_rank < 0)
		{
			$this -> group_rank = 0;
		}
		
		// Add to made changes
		$this -> changes['group_rank'] = $this -> group_rank;
			
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Set group_pos
	 *
	 * @param integer $new_value
	 */
	
	function setGroupPos( $new_value){
		
		// Store in object
		$this -> group_pos = (integer) $new_value;
		
		// Add to made changes
		$this -> changes['group_pos'] = $this -> group_pos;
			
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Set group_hidden
	 *
	 * @param bool $new_value
	 */
	
	function setGroupHidden( $new_value){
		
		// Store in object
		$this -> group_hidden = (integer) $new_value;
		
		// Add to made changes
		$this -> changes['group_hidden'] = $this -> group_hidden;
			
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Set group_masks
	 *
	 * @param array $new_value
	 */
	
	function setGroupMasks( $new_value){
		
		// Store in object
		$this -> group_masks = (array) $new_value;
		
		// Add to made changes
		$this -> changes['group_masks'] = $this -> appCore -> db -> strEscape( join( ',', $this -> group_masks));
			
	}
		
}